package config

import (
	"fmt"
	"github.com/msbranco/goconfig"
	l "util/log"
)

type Config struct {
	Database   string
	Datasource string
	DefPath    string
	DbIp       string
	DbPort     int64
	MaxClient  int64
	TPass      string
	TPort      int64
	AllowIps   []string
	CenterIp   string
	CenterPort int64
}

func (cfg *Config) Load() bool {
	c, err := goconfig.ReadConfigFile("dbserver.cfg")
	if err != nil {
		l.TraceInfo("dbmgr", "load dbserver.cfg error")
		return false
	}

	cfg.Database, _ = c.GetString("database", "db")
	if cfg.Database == "" {
		l.TraceInfo("dbmgr", "dbmgr.cfg database--db must set")
		return false
	}

	cfg.Datasource, _ = c.GetString("database", "datasource")
	cfg.DefPath, _ = c.GetString("system", "path")
	cfg.DbIp, _ = c.GetString("net", "ip")
	cfg.DbPort, _ = c.GetInt64("net", "port")
	cfg.MaxClient, _ = c.GetInt64("net", "maxclient")

	cfg.TPass, _ = c.GetString("telnet", "password")
	cfg.TPort, _ = c.GetInt64("telnet", "port")
	allows, _ := c.GetInt64("telnet", "allowips")
	if allows > 0 {
		cfg.AllowIps = make([]string, allows)
		for i := 0; i < int(allows); i++ {
			cfg.AllowIps[i], _ = c.GetString("telnet", fmt.Sprintf("ip%d", i))
		}
	}
	cfg.CenterIp, _ = c.GetString("center", "ip")
	cfg.CenterPort, _ = c.GetInt64("center", "port")
	return true

}

func CreateConfig() *Config {
	return &Config{}
}
